<?php
/*由bean脚本生成 powerBy--joe*/
namespace app\admin\controller;
use app\common\bean\CategoryBean;
use app\common\bean\CategoryGroupBean;
use app\common\enum\ContentTypeEnum;
use app\common\bean\ContentBean;
use app\common\manage\CategoryGroupManage;
use app\common\manage\CategoryManage;
use app\common\manage\ContentManage;
use app\common\bean\ListMap;
use app\common\utils\ExitJsonUtil;
use app\common\controller\AdminController;
use Exception;

class Content extends AdminController
{
    public function delCate(){
        $cateBean = new CategoryBean();
        $cateBean->setData($this->postParams);
        CategoryManage::getInstance()->delCate($cateBean);
        ExitJsonUtil::getInstance()->exitData([]);
    }
    public function editCate(){
        $cateBean = new CategoryBean();
        $cateBean->setData($this->postParams);
        CategoryManage::getInstance()->editCate($cateBean);
        ExitJsonUtil::getInstance()->exitData([]);
    }
    public function addCate(){
        $cateBean = new CategoryBean();
        $cateBean->setData($this->postParams);
        $cateBean->setStoreId(0);
        list($data['si_id'], $data['name']) = CategoryManage::getInstance()->addCate($cateBean);
        $data['price'] = 10;
        ExitJsonUtil::getInstance()->exitData($data);
    }
	/**
	 * @desc content列表
	 * @return mixed
	 * @throws Exception
	*/
	public function contentList()
	{
		// TODO: Implement getName() method.
        $listMap = new ListMap();
		$listMap->setData($this->getParams);
		$contentMap = ContentManage::getInstance()->getPaginatorList($listMap);
		$this->assign("contentList",$contentMap->getList());
		$this->assign("currentPage",$contentMap->getCurrentPage());
		$this->assign("totalCount",$contentMap->getTotalCount());
		$this->assign('typeSelect',ContentTypeEnum::getArrayName());
		return $this->fetch();

	}

	/**
	 * @desc 编辑content
	 * @throws Exception
	*/
	public  function editContent()
	{
		// TODO: Implement getName() method.
		$contentBean = new ContentBean();
		$contentBean->setData($this->getParams);
		if(request()->isPost()){
			$contentBean = new ContentBean();
			$contentBean->setData($this->postParams);
            $contentBean->setIcon(!is_array($contentBean->getIcon()) ? "" : json_encode($contentBean->getIcon()));
            $contentBean->setPic(!is_array($contentBean->getPic()) ? "" : json_encode($contentBean->getPic()));
			$result = ContentManage::getInstance()->edit($contentBean);
			redirect("editContent?id={$contentBean->getId()}");
		}
		$contentBean = ContentManage::getInstance()->getInfo($contentBean);
		$this->assign('content',$contentBean->getData());
		return $this->fetch();
	}

    /**
     * 设置组合价格
     * @throws Exception
     */
	public function categoryGroupPrice(){
        $categoryGroupBean = new CategoryGroupBean();
	    if(request()->post()){
            $categoryGroupBean->setData($this->postParams);
            CategoryGroupManage::getInstance()->edit($categoryGroupBean);
            ExitJsonUtil::getInstance()->exitData([]);
        }
        $categoryGroupBean->setData($this->getParams);
	    $categoryGroupList = CategoryGroupManage::getInstance()->getStoreTypeCategoryGroup($categoryGroupBean);
	    $this->assign("categoryGroupList",$categoryGroupList);
	    return $this->fetch();
    }
}